#!/usr/bin/env python

import sys, os
from agenda_class import Agenda

def _create_dataset ( start_point, lines, rows, tot_rows ):
        res = {}        
        
        count = 0
        for v in rows:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1
        
        res [ 'rows' ] = tot_rows
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = lines

        return res

def get_calendar ( liwe, id_cal = 0, cal_name = '', uid = 0, gid = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
        """
        :INPUT:
                - id_cal:		SAFE_NUMBER
                - cal_name:		SAFE_DB_STRING
                - uid:			SAFE_NUMBER
                - gid:			SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT: 	SAFE_NUMBER
		- _X_PAGE: 		SAFE_NUMBER
        """

	use_dataset = 0
	quant = 0;

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant
	a = Agenda ( liwe )
	res = a.get_calendar ( id_cal = id_cal, cal_name = cal_name, uid = uid, gid = gid )

	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]

	if not use_dataset:
		cache = {}
		for row in rows: 
			cache [ row [ 'id_cal' ] ] = row
 
		return { 'calendar' : cache }

	return _create_dataset ( start_point, quant, rows, tot_rows )
	
def get_event ( liwe, id_cal = 0, uid = 0 , gid = 0, id_evt = 0, date_start = 0, date_end = 0 ): 
        """
        :INPUT:
                - id_cal:		SAFE_NUMBER
                - uid:			SAFE_NUMBER
                - gid:			SAFE_NUMBER
                - id_evt:		SAFE_NUMBER
                - date_start:		SAFE_DATE
                - date_end:		SAFE_DATE
        """

	a = Agenda ( liwe )
	res = a.get_event ( id_cal = id_cal, uid = uid, gid = gid, id_evt = id_evt, date_start = date_start, date_end = date_end )

	return { 'cal_event' : res }

def get_templates ( liwe ):
	a = Agenda ( liwe )
	return { "templates" : a.get_templates () }
